import store from '@/store'

/**
 * 自定义指令，用于按钮权限控制
 * 使用方法：v-has-permission="['system:user:add']" 存在权限字符串才能看到
 *          v-has-permission="['system:user:add', 'system:user:edit']" 包含权限字符串才能看到
 * @param el
 * @param binding
 */
export default {
    inserted(el, binding) {
        const { value } = binding
        const all_permission = '*:*:*'
        const permissions = store.getters && store.getters.permissions

        if (value && value instanceof Array && value.length > 0) {
            const permissionFlag = value
            const hasPermissions = permissions.some(permission => {
                return all_permission === permission || permissionFlag.includes(permission)
            })
            if (!hasPermissions) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error(`请设置操作权限标签值`)
        }
    }
}
